# -*- coding:utf-8 -*-

from XmlData2DictData import Xml2Dict

from const_data import *
from feature_data.Dict2OrgVenue_Graphfeature import Feature_org_venue_extration
from feature_data.Dict2Venue_Graphfeature import Feature_venue_extration
from feature_data.Dict2Org_Graphfeature import Feature_org_extration

from answer_extration import Answer_extration
from nd_service.first_next_stage_graph import Graph_org_venue_stage
from nd_service.third_stage_graph import Graph_venue_stage
from nd_service.second_stage_graph import Graph_org_stage
from nd_utils.nltk_util import Nltk_util
from feature_data.title_cluster import TitleCluster
from nd_utils.Graph_Cluster_util import *

from nd_utils.venue_re_util import *
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer

class Feature_title_extration():
    def __init__(self, xml2dict, title_param, vectorizer):
        self.xml2dict = xml2dict
        self.nltk_util = Nltk_util()

        self.publication_dict = self.xml2dict.publication_dict
        self.paperidx_list = self.xml2dict.paperidx_list
        self.paperid_idx_dict = self.xml2dict.paperid_idx_dict

        self.paperidx_title_dict = self._get_paperidx_title_dict(param=title_param)
        self.paperidx_venue_dict = self._get_paperidx_venue_dict()
        self.paperidx_org_dict = self._get_paperidx_org_dict()

        self.paperidx_text_dict = self._get_paperidx_text_dict()
        self.previous_stage_clust = self._get_previous_stage_clust()
        # current init graph
        self.init_graph = self._get_cluster_graph()

        self.previous_subgraph_nodes_list = self.previous_stage_clust.previous_subgraph_nodes_list

        # self.countVectorizer = CountVectorizer(stop_words='english')
        self.countVectorizer = CountVectorizer()
        self.countVectorizer.fit_transform(self.paperidx_text_dict.values())
        # self.tfidfVectorizer = TfidfVectorizer(stop_words='english')
        self.tfidfVectorizer = TfidfVectorizer()
        self.tfidfVectorizer.fit_transform(self.paperidx_text_dict.values())

        self.titleCluster_list = self._get_titleCluster_list(vec= vectorizer)
        self.idx_titleCluster_dict = self._get_idx_titleCluster_dict()
        self.idx_titleCluster_vec_dict = self._get_idx_titleCluster_vec_dict()

        """
           由 清理好 的 所有 title 再次 此处 构造 词袋对象，下面构造 TitleCluster 时， 传给它
           CountVector?
            python machine learning
        """

    """
        idx : title, 在此处 清理好 title
    """
    def _get_paperidx_title_dict(self, param='lemma'):
        paper_title_dict = {}
        for paperid, publication_attr_dict in self.publication_dict.items():
            _title = self.nltk_util.stem_process( publication_attr_dict['title'] )
            if param == 'lemma':
                paper_title_dict[ self.paperid_idx_dict[paperid] ] = self.nltk_util.lemma_process_title(_title)
            elif param == 'stem':
                paper_title_dict[self.paperid_idx_dict[paperid]] = self.nltk_util.stem_process_title(_title)

            # paper_title_dict[ self.paperid_idx_dict[paperid] ] = _title

        return paper_title_dict

    def _get_paperidx_org_dict(self):
        paper_org_dict = {}
        for paperid, publication_attr_dict in self.publication_dict.items():
            organization =  publication_attr_dict['organization']
            paper_org_dict[ self.paperid_idx_dict[paperid] ] = get_org_str(organization)
        return paper_org_dict

    def _get_paperidx_venue_dict(self):
        paper_venue_dict = {}
        for paperid, publication_attr_dict in self.publication_dict.items():
            venue =  publication_attr_dict['jconf']
            paper_venue_dict[ self.paperid_idx_dict[paperid] ] = get_clean_paper_venue_bak2(self.nltk_util, venue)
        return paper_venue_dict

    def _get_paperidx_text_dict(self):
        paperidx_text_dict = {}
        for paperidx in self.paperidx_list:
            venue = self.paperidx_venue_dict[paperidx]
            org = self.paperidx_org_dict[paperidx]
            title = self.paperidx_title_dict[paperidx]
            txt_list = [str(title) , str(venue) , str(org)]
            paperidx_text_dict[paperidx] = ' '.join(txt_list)
        return paperidx_text_dict

    def _get_previous_stage_clust(self):
        feature_org_venue_extration = Feature_org_venue_extration(self.xml2dict)
        # feature_venue_extration = Feature_venue_extration(self.xml2dict)
        # feature_org_extration = Feature_org_extration(self.xml2dict)
        answer_extration = Answer_extration(self.xml2dict)
        second_stage_clust = Graph_org_venue_stage(feature_org_venue_extration, answer_extration)
        # second_stage_clust = Graph_venue_stage(feature_venue_extration, answer_extration)

        # second_stage_clust = Graph_org_stage(feature_org_extration, answer_extration)

        return second_stage_clust

    def _get_cluster_graph(self):
        flag = self.previous_stage_clust.simulation_occur_flag
        if flag:
            return self.previous_stage_clust.second_cluster_graph
            # return self.previous_stage_clust.third_cluster_graph
            # return self.previous_stage_clust.second_cluster_graph
        else:
            return self.previous_stage_clust.init_graph

    def _get_titleCluster_list(self, vec = 'count'):
        titleCluster_list = []
        for paperidx_list in self.previous_subgraph_nodes_list:
            if vec == 'count':
                titleCluster = TitleCluster(self.countVectorizer, self.paperidx_text_dict, paperidx_list)
            elif vec == 'tfidf':
                titleCluster = TitleCluster(self.tfidfVectorizer, self.paperidx_text_dict, paperidx_list)

            titleCluster_list.append( titleCluster )
        return titleCluster_list

    def _get_idx_titleCluster_dict(self):
        idx_titleCluster_dict = {}
        for idx , titleCluster in enumerate(self.titleCluster_list):
            idx_titleCluster_dict[idx] = titleCluster
        return idx_titleCluster_dict

    def _get_idx_titleCluster_vec_dict(self):
        idx_titleCluster_vec_dict = {}
        for idx, titleCluster in enumerate(self.titleCluster_list):
            idx_titleCluster_vec_dict[idx] = titleCluster.text_vec
        return idx_titleCluster_vec_dict

"""
    得到 titleCluster_list 之后，
    构造 idx titleCluster 字典
    构造 idx  titleCluster_vec 字典
    

    然后 对 titleCluster_vec 聚类
    
    根据 idx titleCluster 字典 替换 idx
"""

def print_paperid(idx1, idx2):
    xml2dict = Xml2Dict(xml_filepath)
    print xml2dict.idx_paperid_dict[idx1]
    print xml2dict.idx_paperid_dict[idx2]

if __name__ == '__main__':

    print_paperid(45, 71)

    xml2dict = Xml2Dict(xml_filepath)

    print Feature_title_extration(xml2dict).paper_title_dict[18]

"""
xml_file_name2
Feature_org_venue_extration
[ 0.83591331  0.58064516  0.68527919]

Feature_org_extration
[ 0.83125     0.57204301  0.67770701]
Feature_venue_extration
[ 0.6286645   0.62258065  0.62560778]
"""